package tags;

import javax.servlet.ServletException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import dao.DAOFactory;
import dao.IAirportDAO;
import dao.tro.Airport;
import dao.tro.AirportNameAsc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class MainAirportTag extends TagSupport {
    private int airportCount;

    @Override
    public int doStartTag() throws JspException {
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEndTag() throws JspException {
        ArrayList<Airport> airportList= (ArrayList<Airport>) DAOFactory.getDAOFactory(1).getAirportDAO().airportList(airportCount);
        Collections.sort(airportList, new AirportNameAsc());
        for (Airport airport: airportList) {
            try {
                pageContext.setAttribute("singleAirport", airport, PageContext.REQUEST_SCOPE);
                pageContext.include("../Templates/Airport.jsp");
            } catch (ServletException | IOException e) {
                throw new JspException();
            }
        }
        return EVAL_PAGE;
    }

    public int getAirportCount() {
        return airportCount;
    }

    public void setAirportCount(int airportCount) {
        this.airportCount = airportCount;
    }
}
